#include <QDebug>
#include <QtPlugin>
#include <QFileDialog>
#include "ProjectManager.h"
#include "ProjectManager_p.h"
#include "Include/Settings.h"
#include "FindInProjectDlg.h"

namespace Plugins
{
    ProjectManager::ProjectManager(QWidget* parent) :
        QTreeView(parent),
        d_ptr(new ProjectManagerPrivate)
    {
        Q_D(ProjectManager);
        d->q_ptr = this;
        d->init();
    }

    ProjectManager::~ProjectManager()
    {
        //qDebug() << "delete ProjectManager";
        delete d_ptr;
    }

    void ProjectManager::loadProject(const QString & path)
    {
        Q_D(ProjectManager);
        d->loadProject(path);
    }

    void ProjectManager::closeProject()
    {
        Q_D(ProjectManager);
        emit projectAvailable(false);
        d->closeProject();
    }

    void ProjectManager::openProject()
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), Plugins::Settings::instance()->lastUsedWorkspace(), tr("Projects (*.pynotoprj)"));
        if (!fileName.isEmpty()){
            Plugins::Settings::instance()->setLastUsedWorkspace(QFileInfo(fileName).absoluteDir().path());
            loadProject(fileName);
        }
    }

    Project* ProjectManager::project()
    {
        Q_D(ProjectManager);
        return d->_project;
    }

    void ProjectManager::setEditorsManager(Plugins::IEditorsManager * editors)
    {
        Q_D(ProjectManager);
        d->_editors = editors;
    }

    QWidget *ProjectManager::widget()
    {
        return this;
    }

    void ProjectManager::projectProperties()
    {
        Q_D(ProjectManager);
        d->projectProperties();
    }

    void ProjectManager::fileActivated(const QString & path)
    {
        Q_D(ProjectManager);
        d->fileActivated(path);
    }

    void ProjectManager::modifyChanged(const QString & fileName, bool m)
    {
        Q_D(ProjectManager);
        d->modifyChanged(fileName, m);
    }

    void ProjectManager::newProject()
    {
        Q_D(ProjectManager);
        d->projectProperties(true);
    }

    void ProjectManager::reloadTree()
    {
        Q_D(ProjectManager);
        d->reloadTree();
    }

    void ProjectManager::projectRun(IConsole *console)
    {
        Q_D(ProjectManager);
        d->projectRun(console);
    }

    void ProjectManager::navigateFile(const QString &file, int offset)
    {
        emit openFile(file, offset);
        //_project->openFile(file);
    }

    void ProjectManager::navigateFileLine(const QString &file, int line)
    {
        emit openFile(file, line, true);
    }

    void ProjectManager::runFinished()
    {
        sender()->deleteLater();
        emit projectRunned(false);
    }

    void ProjectManager::stopProject()
    {
        Q_D(ProjectManager);
        d->stopProject();
    }

    void ProjectManager::findInProject()
    {
        Q_D(ProjectManager);
        FindInProjectDlg dlg(this);
        if (dlg.exec() == QDialog::Accepted){
            QStringList list = d->searchInProject(dlg.search(), dlg.filePattern(), dlg.isCase(), dlg.isWhole(), dlg.isRegular());
            emit filesFound(list);
        }
    }
}

Q_EXPORT_PLUGIN2(projectmanager, Plugins::ProjectManager)
